package com.xfb.test;

import com.xfb.jdbc.io.Resources;
import com.xfb.jdbc.sqlSession.Session;
import com.xfb.jdbc.sqlSession.SqlSessionBuilder;
import com.xfb.jdbc.sqlSession.SqlSessionFactory;
import com.xfb.test.dao.UserDao;
import com.xfb.test.pojo.User;

import java.io.InputStream;
import java.util.List;

/**
 * @author xing fang bin
 * @date 2021/6/27 0027 22:43
 * @description
 */
public class test {

    public static void main(String[] args) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionBuilder().build(resourceAsStream);
        Session session = sqlSessionFactory.openSession();
        /******************************  传统方式直接调用直接调用  **************************************/
        /*User user = new User();
        user.setId(1);
        user.setUsername("邢芳彬");
        User selectOne = session.selectOne("user.selectOne", user);
        System.out.println(selectOne);*/
        /*List<User> userList = session.selectList("user.selectList");
        for (User user : userList) {
            System.out.println(user);
        }*/

        // 使用代理模式调用
        // 返回的是一个代理对象，使用代理对象调用接口中的任意方法时，都会执行invoke方法
        /*UserDao userDao = session.getMapper(UserDao.class);
        List<User> userList = userDao.selectList();
        for (User user : userList) {
            System.out.println(user);
        }

        User user = new User();
        user.setId(1);
        user.setUsername("邢芳彬");
        User selectOne = userDao.selectOne(user);
        System.out.println(selectOne);*/

        /*User user = new User();
        user.setId(2);
        user.setUsername("马聪聪");
        Integer add1 = session.add("com.xfb.test.dao.UserDao.add", user);
        if (add1 > 0) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }*/

        /*User user = new User();
        user.setId(2);
        user.setUsername("邢彬");
        Integer add1 = session.update("com.xfb.test.dao.UserDao.update", user);
        if (add1 > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }*/

        /*Integer add1 = session.update("com.xfb.test.dao.UserDao.delete",2);
        if (add1 > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }*/
        /******************************  传统方式直接调用直接调用 END **************************************/


        /******************************  使用getMapper方式调用  **************************************/
        UserDao userDao = session.getMapper(UserDao.class);
        User user = new User();
/*        user.setId(2);
        user.setUsername("唐三");
        Integer add = userDao.add(user);
        if (add > 0) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }*/

/*        user.setId(2);
        user.setUsername("小舞");
        Integer update = userDao.update(user);
        if (update > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }*/


        Integer update = userDao.delete(2);
        if (update > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }
}
